package com.ios.service.impl;
import com.ios.dao.DisablerUserMapper;
import com.ios.dao.LoginRecordMapper;
import com.ios.dao.RelativeUserMapper;
import com.ios.dto.RelativeExecution;
import com.ios.entity.DisablerUser;
import com.ios.entity.LoginRecord;
import com.ios.entity.RelativeUser;
import com.ios.enums.RelativeStateEnum;
import com.ios.exceptions.RelativeOperationException;
import com.ios.service.IRelativeService;
import com.ios.util.ImageUtil;
import com.ios.util.PathUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
@Service
public class RelativeServiceImpl implements IRelativeService {

    @Autowired
    private RelativeUserMapper relativeUserMapper;
    @Autowired
    private DisablerUserMapper disablerUserMapper;
    @Autowired
    private LoginRecordMapper loginRecordMapper;

    @Override
    public RelativeExecution RegisterRelativeUser(String username, String password_1, String password_2) {
        RelativeUser relativeUser = new RelativeUser();
        if (username.equals(null)||password_1.equals(null)||password_2.equals(null)){
            return new RelativeExecution(RelativeStateEnum.PARAM_NULL);
        }else {
            try{
                if (checkUsername(username)){
                    return new RelativeExecution(RelativeStateEnum.REPEAT_USERNAME);
                }else if (!password_1.equals(password_2)){
                    return new RelativeExecution(RelativeStateEnum.PASSWORD_ERROR);
                }else {
                    relativeUser.setUsername(username);
                    relativeUser.setPassword(password_1);
                    relativeUserMapper.insertSelective(relativeUser);
                    return new RelativeExecution(RelativeStateEnum.SUCCESS,relativeUser);
                }
            }catch (Exception e){
                return new RelativeExecution(RelativeStateEnum.FAIL);
            }
        }
    }

    @Override
    public RelativeExecution CompleteRegisterRelativeUser(RelativeUser relativeUser, InputStream HeadImgInputStream, String fileName) {
     /*判断传入残疾人用户信息是否为空*/
        if (relativeUser == null) {
            return new RelativeExecution(RelativeStateEnum.PARAM_NULL);
        } else if (checkPhone(relativeUser)) {
            /*判断用户手机号是否重复*/
            return new RelativeExecution(RelativeStateEnum.REPEAT_PHONE);
        } else if(!checkDisabler(relativeUser)){
            /*判断被监护人是否存在*/
            return new RelativeExecution(RelativeStateEnum.NULL_DISABLER);
        } else{
            /*给用户注册时间信息赋值*/
            relativeUser.setCreateTime(new Date());
            relativeUser.setUpdateTime(new Date());
            relativeUserMapper.updateByPrimaryKeySelective(relativeUser);
            if (HeadImgInputStream != null) {/*存储图片*/
                try {
                    relativeUser.setHeadImage(addrelativeHeadImg(relativeUser, HeadImgInputStream, fileName));
                    relativeUserMapper.updateHeadImg(addrelativeHeadImg(relativeUser, HeadImgInputStream, fileName), relativeUser.getUsername());
                } catch (Exception e) {
                    throw new RelativeOperationException("register error:" + e.getMessage());
                }
            }
            return new RelativeExecution(RelativeStateEnum.SUCCESS, relativeUser);
        }
    }

    @Override
    public RelativeExecution LoginRelativeUser(String username, String password) {
        if (username.equals(null)||password.equals(null)){
            return new RelativeExecution(RelativeStateEnum.PARAM_NULL);
        }else{
            try{
                LoginRecord loginRecord = new LoginRecord();
                loginRecord.setUsername(username);
                loginRecord.setPassword(password);
                loginRecord.setStatus(0);
                loginRecord.setLoginTime(new Date());
                loginRecord.setResult("待验证");
                loginRecordMapper.insert(loginRecord);
                return new RelativeExecution(RelativeStateEnum.SUCCESS,loginRecord);
            }catch (Exception e){
                return new RelativeExecution(RelativeStateEnum.FAIL);
            }
        }
    }

    @Override
    public RelativeExecution CheckLoginRelativeUser(int id) {
        if (id==-1){
            return new RelativeExecution(RelativeStateEnum.PARAM_NULL);
        }else {
            LoginRecord loginRecord = loginRecordMapper.selectbyId(id);
            if (!checkUsername(loginRecord.getUsername())){
                loginRecord.setResult("登录失败");
                loginRecordMapper.update(loginRecord);
                return new RelativeExecution(RelativeStateEnum.ERROR_USERNAME);
            }else {
                RelativeUser relativeUser = relativeUserMapper.selectByUsername(loginRecord.getUsername());
                if (relativeUser.getPassword().equals(loginRecord.getPassword())){
                    loginRecord.setResult("登录成功");
                    loginRecordMapper.update(loginRecord);
                    return new RelativeExecution(RelativeStateEnum.SUCCESS, relativeUser);
                }else {
                    loginRecord.setResult("登录失败");
                    loginRecordMapper.update(loginRecord);
                    return new RelativeExecution(RelativeStateEnum.ERROR_PASSWORD);
                }
            }
        }
    }

    @Override
    public RelativeExecution QueryRelativeUser(String username) {
        RelativeUser relativeUser = null;
        if (!checkUsername(username)){
            /*用户不存在*/
            return new RelativeExecution(RelativeStateEnum.ERROR_USERNAME);
        }else {
            /*用户存在*/
            try{
                relativeUser = relativeUserMapper.queryByUsername(username);
                return new RelativeExecution(RelativeStateEnum.SUCCESS,relativeUser);
            }catch (Exception e){
                return new RelativeExecution(RelativeStateEnum.INNER_ERROR);
            }
        }
    }

    @Override
    public RelativeExecution EditRelativeInfo(RelativeUser relativeUser , InputStream HeadImgInputStream, String fileName) {
        if (checkPhone(relativeUser)) {
            /*判断新用户手机号是否重复*/
            return new RelativeExecution(RelativeStateEnum.REPEAT_PHONE);
        } else if (checkUsername(relativeUser)) {
            /*判断新用户名是否重复*/
            return new RelativeExecution(RelativeStateEnum.REPEAT_USERNAME);
        } else if(!checkDisabler(relativeUser)){
            /*判断新被监护人是否存在*/
            return new RelativeExecution(RelativeStateEnum.NULL_DISABLER);
        } else{
            try {
                /*判断图片*/
                if (HeadImgInputStream != null){
                    /*添加新图片*/
                    relativeUser.setHeadImage(addrelativeHeadImg(relativeUser, HeadImgInputStream, fileName));
                    relativeUserMapper.updateHeadImg(addrelativeHeadImg(relativeUser, HeadImgInputStream, fileName), relativeUser.getUsername());
                }
                /*给用户更新信息时间赋值*/
                relativeUser.setUpdateTime(new Date());
                relativeUserMapper.updateByPrimaryKeySelective(relativeUser);
            } catch (Exception e) {
                return new RelativeExecution(RelativeStateEnum.FAIL);
            }
            return new RelativeExecution(RelativeStateEnum.SUCCESS, relativeUser);
        }
    }

    /*增加图片*/
    private String addrelativeHeadImg(RelativeUser relativeUser, InputStream HeadImgInputStream, String fileName){
        /*获取头像图片目录的相对路径*/
        String dest = PathUtil.getRealtiveHeadImgPath();
        String relativeHeadImgAddr = ImageUtil.ThumbnailsImg(HeadImgInputStream,fileName,dest);
        return relativeHeadImgAddr;
    }

    /*判断用户手机号是否重复*/
    private boolean checkPhone(RelativeUser relativeUser){
        List<String> listPhone = relativeUserMapper.getAllPhone();
        for (String phone:listPhone) {
            if (relativeUser.getPhoneNo().equals(phone)) {
                return true;
            }
        }
        return false;
    }

    /*判断用户是否存在*/
    private boolean checkUsername(String username){
        List<String> listUsername = relativeUserMapper.getAllUsername();
        for (String username_1:listUsername) {
            if (username_1.equals(username)) {
                return true;
            }
        }
        return false;
    }
    /*判断用户名是否重复*/
    private boolean checkUsername(RelativeUser relativeUser){
        List<String> listUsername = relativeUserMapper.getAllUsername();
        for (String username:listUsername) {
            if (relativeUser.getUsername().equals(username)) {
                return true;
            }
        }
        return false;
    }

    /*判断被监护人是否存在*/
    private boolean checkDisabler(RelativeUser relativeUser) {
        List<Integer> listDisablerId = disablerUserMapper.getAllDisablerId();
        for (Integer id:listDisablerId) {
            if (relativeUser.getDisablerId()==id){
                return true;
            }
        }
        return false;
    }

}

